//
//  SAInputDiscountView.m
//  saasRetail
//
//  Created by 黄志刚 on 2021/4/22.
//

#import "SAInputDiscountView.h"

@interface SAInputDiscountView ()<UITextFieldDelegate>

@property (nonatomic, strong) NSString *topTitle;
@property (nonatomic, assign) CGFloat discountRate;
@property (nonatomic, assign) CGFloat discountPrice;
@property (nonatomic, assign) CGFloat retailPrice;
@property (nonatomic, strong) UITextField *discountRateTf;
@property (nonatomic, strong) UITextField *discountPriceTf;
@property (nonatomic, strong) NSString *priceType;

@end

@implementation SAInputDiscountView

+(instancetype)initViewTitle:(NSString *)title DiscountRate:(CGFloat)discountRate DiscountPrice:(CGFloat)discountPrice AndRetailPrice:(CGFloat)retailPrice {
    SAInputDiscountView * shareView = [[SAInputDiscountView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    shareView.topTitle = title;
    shareView.retailPrice = retailPrice / 100;
    shareView.discountRate = discountRate * 100;
    shareView.discountPrice = discountPrice/100;
    [shareView creatUI];
    return shareView;
}

-(instancetype)initWithFrame:(CGRect)frame {
    if(self = [super initWithFrame:frame]) {
        self.userInteractionEnabled = YES;
        self.backgroundColor = kColor(0, 0, 0, 0.6);
    }
    return self;
}

-(void)leftButtonClick:(UIButton *)sender {
    [self shareDissTapEvent];
}

-(void)rightButtonClick:(UIButton *)sender {
    if ([self.priceType isEqualToString:@"-1"]) {
        [self shareDissTapEvent];
        return;
    }
    
    CGFloat discountRate = 0;
    CGFloat discountPrice = 0;
    if (self.discountRateTf.text.length == 0) {
        discountRate = 1;
    }else {
        discountRate = [self.discountRateTf.text floatValue]/100;
    }
    if (self.discountPriceTf.text.length == 0) {
        discountPrice = self.retailPrice;
    }else {
        discountPrice = [self.discountPriceTf.text floatValue] * 100;
    }
    
    !self.discountBlock ?: self.discountBlock(discountRate, discountPrice, self.priceType);
    [self shareDissTapEvent];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (string.length == 0) {
        return YES;
    }
    if (textField.text.length > 7) {
        return NO;
    }
    // 如果第一位是.则前面加上0.
    if((textField.text.length == 0) && [string isEqualToString:@"."]) {
        textField.text = @"0";
    }
    if([textField.text containsString:@"."]) {
        if ([string isEqualToString:@"."]) {
            return NO;
        }
        NSRange ran = [textField.text rangeOfString:@"."];
        if(range.location > ran.location) {
            if([textField.text pathExtension].length > 1) {
                return NO;
            }
        }
    }
//    限制折扣100000%
    if (self.discountRateTf == textField) {
        if ([self validateNumber:string]) {
            NSString *newStr = [NSString stringWithFormat:@"%@%@",textField.text,string];
            if ([newStr floatValue] > 10000) {
                return NO;
            }
        }
    }
    if (self.discountPriceTf == textField) {
        if ([self validateNumber:string]) {
            NSString *newStr = [NSString stringWithFormat:@"%@%@",textField.text,string];
            if ([newStr doubleValue] <= self.retailPrice * 100) {
                return YES;
            }else {
                return NO;
            }
        }
    }
    return [self validateNumber:string];
}

- (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@".0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}

- (void)textFieldEditChanged:(UITextField*)textField {
    if (self.discountRateTf == textField) {
        self.priceType = @"1";
        if (textField.text.length == 0) {
            self.discountPriceTf.text = [NSString stringWithFormat:@"%0.2f",self.retailPrice];
        }else{
            double rate = [self.discountRateTf.text doubleValue];
            double discountPrice = rate * self.retailPrice;
            NSString *discountPriceStr = [NSString stringWithFormat:@"%0.2lf",round(discountPrice)/100];
            self.discountPriceTf.text = discountPriceStr;
        }
    }else {
        self.priceType = @"0";
        if (textField.text.length == 0) {
            self.discountRateTf.text = @"100.00";
        }else {
            if (self.retailPrice == [self.discountPriceTf.text doubleValue]) {
                self.discountRateTf.text = @"100.00";
            }else {
                if (self.retailPrice == 0) {
                    self.discountRateTf.text = @"100.00";
                }else {
                    self.discountRateTf.text = [NSString stringWithFormat:@"%0.2f",round([self.discountPriceTf.text floatValue] / self.retailPrice * 10000.f)/100];
                }
            }
        }
    }
}

#pragma mark - initUI
-(void)creatUI {
    UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    deleteBtn.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    [deleteBtn addTarget:self action:@selector(shareDissTapEvent) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:deleteBtn];
    
    UIView *backGroundView = [[UIView alloc] initWithFrame:CGRectMake(kScaleNum(40), kScaleNum(150), kScreenWidth - kScaleNum(80), kScaleNum(200))];
    backGroundView.backgroundColor = [UIColor whiteColor];
    backGroundView.userInteractionEnabled = YES;
    [self addSubview:backGroundView];
    
    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth - kScaleNum(80), kScaleNum(40))];
    titleLab.text = @"单品折扣";
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.textColor = kBlackColor;
    titleLab.font = kFont(16);
    [backGroundView addSubview:titleLab];
    if (self.topTitle.length > 0) {
        titleLab.text = self.topTitle;
    }
    
    UILabel *discountRateLab = [[UILabel alloc] initWithFrame:CGRectMake(kScaleNum(25), kScaleNum(45), kScaleNum(80), kScaleNum(40))];
    discountRateLab.text = @"折扣率";
    discountRateLab.textAlignment = NSTextAlignmentLeft;
    discountRateLab.textColor = kBlackColor;
    discountRateLab.font = kFont(16);
    [backGroundView addSubview:discountRateLab];
    
    UITextField *discountRateTf = [[UITextField alloc] initWithFrame:CGRectMake(kScaleNum(105), kScaleNum(45), kScreenWidth - kScaleNum(230), kScaleNum(40))];
    discountRateTf.placeholder = @"请输入折扣率";
    discountRateTf.font = kFont(15);
    discountRateTf.textColor = kBlackColor;
    discountRateTf.textAlignment = NSTextAlignmentLeft;
    discountRateTf.clearButtonMode = UITextFieldViewModeWhileEditing;
    discountRateTf.layer.cornerRadius = kScaleNum(5);
    discountRateTf.layer.masksToBounds = YES;
    discountRateTf.layer.borderColor = kColor(220, 220, 220, 1).CGColor;
    discountRateTf.layer.borderWidth = kScaleNum(1);
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScaleNum(10), kScaleNum(40))];
    discountRateTf.leftViewMode = UITextFieldViewModeAlways;
    discountRateTf.leftView = leftView;
    [backGroundView addSubview:discountRateTf];
    discountRateTf.delegate = self;
    discountRateTf.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    [discountRateTf becomeFirstResponder];
    self.discountRateTf = discountRateTf;
    [discountRateTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    discountRateTf.text = [NSString stringWithFormat:@"%0.2f",self.discountRate];
    
    UILabel *discountRateLab2 = [[UILabel alloc] initWithFrame:CGRectMake(kScaleNum(260), kScaleNum(45), kScaleNum(20), kScaleNum(40))];
    discountRateLab2.text = @"%";
    discountRateLab2.textAlignment = NSTextAlignmentLeft;
    discountRateLab2.textColor = kBlackColor;
    discountRateLab2.font = kFont(15);
    [backGroundView addSubview:discountRateLab2];
    
    UILabel *discountPriceLab = [[UILabel alloc] initWithFrame:CGRectMake(kScaleNum(25), kScaleNum(97), kScaleNum(80), kScaleNum(40))];
    discountPriceLab.text = @"折扣金额";
    discountPriceLab.textAlignment = NSTextAlignmentLeft;
    discountPriceLab.textColor = kBlackColor;
    discountPriceLab.font = kFont(16);
    [backGroundView addSubview:discountPriceLab];
    
    UITextField *discountPriceTf = [[UITextField alloc] initWithFrame:CGRectMake(kScaleNum(105), kScaleNum(97), kScreenWidth - kScaleNum(230), kScaleNum(40))];
    discountPriceTf.placeholder = @"请输入折扣金额";
    discountPriceTf.font = kFont(15);
    discountPriceTf.textColor = kBlackColor;
    discountPriceTf.textAlignment = NSTextAlignmentLeft;
    discountPriceTf.clearButtonMode = UITextFieldViewModeWhileEditing;
    discountPriceTf.layer.cornerRadius = kScaleNum(5);
    discountPriceTf.layer.masksToBounds = YES;
    discountPriceTf.layer.borderColor = kColor(220, 220, 220, 1).CGColor;
    discountPriceTf.layer.borderWidth = kScaleNum(1);
    UIView *leftView2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScaleNum(10), kScaleNum(40))];
    discountPriceTf.leftViewMode = UITextFieldViewModeAlways;
    discountPriceTf.leftView = leftView2;
    [backGroundView addSubview:discountPriceTf];
    discountPriceTf.delegate = self;
    discountPriceTf.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    [discountPriceTf becomeFirstResponder];
    self.discountPriceTf = discountPriceTf;
    [discountPriceTf addTarget:self action:@selector(textFieldEditChanged:) forControlEvents:UIControlEventEditingChanged];
    discountPriceTf.text = [NSString stringWithFormat:@"%0.2f",self.discountPrice];
    
    UILabel *discountPriceLab2 = [[UILabel alloc] initWithFrame:CGRectMake(kScaleNum(260), kScaleNum(97), kScaleNum(20), kScaleNum(40))];
    discountPriceLab2.text = @"元";
    discountPriceLab2.textAlignment = NSTextAlignmentLeft;
    discountPriceLab2.textColor = kBlackColor;
    discountPriceLab2.font = kFont(15);
    [backGroundView addSubview:discountPriceLab2];
    
    UIButton *cancelCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelCodeBtn.frame = CGRectMake(kScaleNum(0),kScaleNum(155), kScaleNum(147), kScaleNum(45));
    cancelCodeBtn.titleLabel.font = kFont(15);
    [cancelCodeBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelCodeBtn setTitleColor:kGrayColor forState:UIControlStateNormal];
    [cancelCodeBtn addTarget:self action:@selector(leftButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [backGroundView addSubview:cancelCodeBtn];
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmBtn.frame = CGRectMake(kScaleNum(148),kScaleNum(155), kScaleNum(147), kScaleNum(45));
    confirmBtn.titleLabel.font = kFont(15);
    [confirmBtn setTitle:@"确认" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:kBuleColor forState:UIControlStateNormal];
    [confirmBtn addTarget:self action:@selector(rightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [backGroundView addSubview:confirmBtn];
    
    UIView *line = [[UIView alloc] initWithFrame:SARectMake(0, 155, 295, 1)];
    line.backgroundColor = kLineColor;
    [backGroundView addSubview:line];
    
    UIView *line2 = [[UIView alloc] initWithFrame:SARectMake(147, 155, 1, 45)];
    line2.backgroundColor = kLineColor;
    [backGroundView addSubview:line2];
    
    self.priceType = @"-1";
}

-(void)shareDissTapEvent
{
    [UIView animateWithDuration:0.5f animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
